//
//  UploadView.m
//  carcareIOS
//
//  Created by tianxing on 16/1/26.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "UploadView.h"
typedef NS_ENUM(NSInteger , ViewsType)
{
    ViewType_BackV = 10,
    ViewType_ImageV,
    ViewType_Label
};

@interface UploadView()
@property (nonatomic ,strong) UIImageView * licenseImageView;
@property (nonatomic ,strong) UIButton * uploadButton;
@end
@implementation UploadView
- (instancetype)initWithUploadLicenseName:(NSString *)Name andDescripeDetail:(NSString *)descripeDetail addImageName:(NSString *)addImageName uploadBtnTitle:(NSString *)uploadBtnTitle{
    self = [self init];
    if (self) {
        self.frame = CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64);
        [self initWithLicenseName:Name andDescripeDetail:descripeDetail  addImageName:addImageName uploadBtnTitle:uploadBtnTitle];
    }
    return self;
}
- (void)initWithLicenseName:(NSString *)LicenseName andDescripeDetail:(NSString *)descripeDetail addImageName:(NSString *)addImageName uploadBtnTitle:(NSString *)uploadBtnTitle{
    
    self.backgroundColor = COLOR_HEXSTRING(@"#FFFFFF");
    NSString *License = [NSString stringWithFormat:@"%@",LicenseName];
    
    UIView * sectionView = [[[UIView viewWithFrame:CGRectMake(- 0.5, 0, __MAIN_WIDTH + 1, 14 * __MAIN_RATIO_H_667)] resetBackgroundColor:[Theme backGroundColor]] setupOnView:self];
    sectionView.layer.borderWidth = 0.5;
    sectionView.layer.borderColor = COLOR_HEXSTRING(@"#D0D0D0").CGColor;
    
    // 指定的证件
    UILabel *licenseLabel = [Factory labelWithFrame:CGRectMake(21.5 * __MAIN_RATIO_375, 48 * __MAIN_RATIO_H_667, __MAIN_WIDTH, 19 * __MAIN_RATIO_H_667) font:FONT_DEFAULT_Light(13) text:License textColor:COLOR_HEXSTRING(@"#D6D6D6") onView:self textAlignment:NSTextAlignmentLeft];
    
    UIView * backView = [[[UIView viewWithFrame:CGRectMake(21.5 * __MAIN_RATIO_375, licenseLabel.bottom + 19 *__MAIN_RATIO_H_667, 331.5 * __MAIN_RATIO_375, 288 * __MAIN_RATIO_H_667)] resetBackgroundColor:COLOR_HEXSTRING(@"#F4F4F4")] setupOnView:self];
    backView.layer.borderWidth = 0.5;
    backView.layer.borderColor = COLOR_HEXSTRING(@"#DCDCDC").CGColor;
    backView.layer.cornerRadius = 4;
    backView.layer.masksToBounds = YES;
    backView.tag = ViewType_BackV;
    // 上传的图片
    self.licenseImageView = [Factory imageViewWithFrame:CGRectMake(10 * __MAIN_RATIO_375, 5, backView.frame.size.width - 10 * 2 * __MAIN_RATIO_375, backView.frame.size.height - 10) image:nil onView:backView];
    self.licenseImageView.backgroundColor = [UIColor clearColor];
        self.licenseImageView.contentMode = UIViewContentModeScaleToFill;
    self.licenseImageView.layer.cornerRadius = 4;
    self.licenseImageView.layer.masksToBounds = YES;
    [backView bringSubviewToFront:self.licenseImageView];
    
    // 添加图片
    UIImage * instureAddImage = ImageNamed(addImageName);
    CGFloat centerImageAndLabelY = 20 * __MAIN_RATIO_375;
    CGFloat imageCenterY = backView.height / 2 - instureAddImage.size.height / 2 ;
    UIImageView * centerImageV = [Factory imageViewWithCenter:CGPointMake(backView.width / 2, imageCenterY) image:instureAddImage onView:backView];
    centerImageV.tag = ViewType_ImageV;
    
    CGFloat labelH = 60 * __MAIN_RATIO_375;
    CGFloat labelX = 10 * __MAIN_RATIO_375;
    UILabel * centerLabel = [Factory labelWithFrame:CGRectMake(labelX , centerImageV.bottom + centerImageAndLabelY, backView.width - labelX * 2, labelH) font:FONT_DEFAULT_Light(17) text:descripeDetail textColor:COLOR_HEXSTRING(@"#A3A3A3") onView:backView textAlignment:NSTextAlignmentCenter];
    centerLabel.backgroundColor = [UIColor clearColor];
    centerLabel.tag = ViewType_Label;
    
    UIButton * addButton = [[UIButton buttonWithFrame:backView.frame title:@"" textColor:[UIColor clearColor] font:FONT_BUTTON click:nil] setupOnView:self];
    addButton.clipsToBounds = YES;
    addButton.layer.cornerRadius = 4;
    [addButton addTarget:self action:@selector(addBtAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 上传
    self.uploadButton = [[[UIButton buttonWithFrame:CGRectMake(21.5 *__MAIN_RATIO_375, backView.bottom +  60 * __MAIN_RATIO_H_667, 329 * __MAIN_RATIO_375, 50 * __MAIN_RATIO_H_667) title:uploadBtnTitle textColor:[UIColor whiteColor] font:FONT_BUTTON click:nil] resetBackgroundColor:COLOR_HEXSTRING(@"#A1A1A1")] setupOnView:self];
    self.uploadButton.clipsToBounds = YES;
    self.uploadButton.layer.cornerRadius = 4;
    [self.uploadButton addTarget:self action:@selector(upLoadAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel * headLabel = [Factory labelWithFrame:CGRectMake(22 * __MAIN_RATIO_375, self.uploadButton.bottom + 10 , __MAIN_WIDTH - 22 * __MAIN_RATIO_375 * 2, 20) font:FONT_DEFAULT_Light(11) text:@"车挣不会通过任何渠道泄露您的个人信息，请放心填写" textColor:COLOR_HEXSTRING(@"#D6D6D6") onView:self textAlignment:NSTextAlignmentLeft];
    headLabel.numberOfLines = 2;
}

- (void)setUpImage:(UIImage *)upImage{
    _upImage = upImage;
    if (upImage != nil) {
        self.licenseImageView.image = upImage;
        CGFloat add = self.licenseImageView.width / upImage.size.width * upImage.size.height - self.licenseImageView.height;
        self.licenseImageView.height += add;
        
        UIView * backView = [self viewWithTag:ViewType_BackV];
        backView.top -= add;
        backView.height += add;
        UIImageView * addImageV = (UIImageView *)[backView viewWithTag:ViewType_ImageV];
        addImageV.hidden = YES;
        UILabel * addlabel = (UILabel *)[backView viewWithTag:ViewType_Label];
        addlabel.hidden = YES;
        
        self.uploadButton.backgroundColor = [Theme mainNavColor];
    }
}

- (void)addBtAction:(UIButton *)sender{
    if(self.addbuttonClick) self.addbuttonClick(sender);
}

- (void)upLoadAction:(UIButton *)sender{
    if (self.licenseImageView.image) {
        if (self.commitbuttonClick) self.commitbuttonClick(sender);
    }else [AlertMgr showShortWarnTextTips:@[@"请先选择上传图片哦!"]];
}

@end
